﻿using Microsoft.Win32;
using System;
using System.Configuration;
using System.Windows;

namespace BrowserXP
{
    class BrowserApp : Application
    {
        const string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";

        protected override void OnStartup(StartupEventArgs e)
        {
            addResources();
            try
            {
                string production = ConfigurationManager.AppSettings["production1"];
                if (production != null && production == "true")
                {
                    string execPath = System.Windows.Forms.Application.ExecutablePath;
                    //开机启动注册表
                    if (RegistryHelper.IsRegistryExist(Registry.LocalMachine, subKey, "BrowserXP"))
                    {
                        //如果出册表存在，需判断是否未当前值，不是则修改
                        if (!RegistryHelper.GetRegistryData(Registry.LocalMachine, subKey, "BrowserXP").Equals(execPath))
                            RegistryHelper.SetRegistryData(Registry.LocalMachine, subKey, "BrowserXP", execPath);
                    }
                    else
                    {
                        //入则表不存在的直接写入
                        RegistryHelper.SetRegistryData(Registry.LocalMachine, subKey, "BrowserXP", execPath);
                    }
                    string pathlist;
                    pathlist = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
                    string[] list = pathlist.Split(';');
                    bool isPathExist = false;
                    execPath = execPath.Substring(0, execPath.LastIndexOf("BrowserXP"));
                    foreach (string item in list)
                    {
                        if (item == execPath)
                            isPathExist = true;
                    }
                    if (!isPathExist)
                    {
                        Environment.SetEnvironmentVariable("PATH", pathlist + ";" + execPath, EnvironmentVariableTarget.Machine);
                    }
                }
                base.OnStartup(e);

                if (e.Args.Length > 0)
                    ShowBusinessWindow(e.Args[0]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("[注册表操作/环境变量]向注册表写开机启动信息或者写入环境变量失败, Exception: {0}", ex.Message));
            }
        }

        private void addResources()
        {
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/GlobalStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiButtonStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiTextBlockStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiToggleButtonStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiCheckBoxStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiDatePickerStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiScrollViewerStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiListBoxStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiComboBoxStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiMenuStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiTreeViewStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/HiProgressRingStyle.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/SlickButtonRD.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/MainGridStyleRD.xaml", UriKind.Relative)) as ResourceDictionary);
            base.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("Styles/StealthButtonRD.xaml", UriKind.Relative)) as ResourceDictionary);
        }

        public void ShowBusinessWindow(string url)
        {
            try
            {
                BrowserWindow window = new BrowserWindow(url);
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //根据Key读取元素的Value
                string name = config.AppSettings.Settings["url"].Value;
                //写入元素的Value
                config.AppSettings.Settings["url"].Value = url;
                //一定要记得保存，写不带参数的config.Save()也可以
                config.Save(ConfigurationSaveMode.Modified);
                //刷新，否则程序读取的还是之前的值（可能已装入内存）
                ConfigurationManager.RefreshSection("appSettings");
                window.Show();
            }
            catch
            {
                MessageBox.Show("无法加载文件.");
            }
        }
    }
}
